<?php

namespace App\Services;

use App\Models\Cms_Member_Zone;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ZoneService
{
    public function zoneList($params)
    {
        $result = Cms_Member_Zone::getFilter($params)
            ->Nodel()
            ->orderBy('created_at','desc')
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function insertZone($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $time = Carbon::now()->timestamp;
            $params['create_time']  = $time;
            $params['update_time']  = $time;
            $result = Cms_Member_Zone::create($params);
            if(!$result)
                throw new \Exception("新增Cms_Member_Zone失败".$params['id']);

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function updateZone($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $time = Carbon::now()->timestamp;
            $params['update_time']  = $time;
            $result = Cms_Member_Zone::where('id',$params['id'])->update($params);
            if(!$result){
                throw new \Exception("更新Cms_Member_Zone失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function zoneDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Cms_Member_Zone::find($params['id']);
            $res->is_del = 1;
            $res->update_time = Carbon::now()->timestamp;
            if(!$res->save()){
                throw new \Exception("删除Cms_Member_Zone失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function zoneStatusChange($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Cms_Member_Zone::find($params['id']);
            $res->status = $params['status'];
            $res->update_time = Carbon::now()->timestamp;
            if(!$res->save()){
                throw new \Exception("更新Cms_Member_Zone失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
